<template>
    <van-form @submit="submit">
        <component
                v-for="(component, index) in data.components"
                :key="index"
                :is="component.render.type"
                v-bind:data="component.data"
        >
        </component>
    </van-form>
</template>

<script>
    import { getFieldData } from './data'
    import { message } from "ant-design-vue"
    import Vue from 'vue'
    import VueCookies from 'vue-cookies'
    Vue.use(VueCookies);

    export default {
        components: {

        },
        props : {
            data : Object
        },
        data() {
            return {
                subId : ''
            }
        },
        methods:{
            async submit() {
                if(this.subId !== '' || this.$cookies.isKey(this.data.id)){
                    message.error('您已提交过该表单, 请勿重复提交');
                    return;
                }
                let resp = await this.$post('/api/form/submit', getFieldData(this.data));
                this.subId = resp.sub_id;
                this.$cookies.set(this.data.id, this.subId, "30d");
                message.success('提交成功')
            },
        }
    }
</script>